using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class OpenidDAL
    {
        public static int Save(OpenidInfo info)
        {
            string sql = string.Format("insert into [openid]([YourOpenID],[OurOpenID],[AuthorizationAppID],[CreateDate]) values(@YourOpenID,@OurOpenID,@AuthorizationAppID,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@YourOpenID", info.YourOpenID),
                new SqlParameter("@OurOpenID", info.OurOpenID),
                new SqlParameter("@AuthorizationAppID", info.AuthorizationAppID),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static int Update(OpenidInfo info)
        {
            string sql = string.Format("update [openid] set [YourOpenID]=@YourOpenID,[OurOpenID]=@OurOpenID where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@YourOpenID", info.YourOpenID),
                new SqlParameter("@OurOpenID", info.OurOpenID),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }

        public static List<OpenidInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[openid]");
            return ListModel(dt);
        }
        public static List<OpenidInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [openid].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [openid].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[openid]", out count);
            return ListModel(dt);
        }
        public static List<OpenidInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [openid]"));
            return ListModel(dt);
        }
        public static bool IsExist(string youropenid, string ouropenid)
        {
            int count =DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select count(1) from [openid] where YourOpenID='{0}' and OurOpenID='{1}'", youropenid, ouropenid)));
            return count > 0 ? true : false;
        }
        public static string FindOurOpenID(string youropenid, string authorizationappid)
        {
            return DataConvert.ToString(CommonDAL.ExecuteScalar(string.Format("select ouropenid from [openid] where YourOpenID='{0}' and AuthorizationAppID='{1}'", youropenid, authorizationappid)));
        }
        private static OpenidInfo Model(DataRow dr, DataColumnCollection col)
        {
            OpenidInfo info = new OpenidInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.YourOpenID = DataConvert.ToString(dr["YourOpenID"]);
                info.OurOpenID = DataConvert.ToString(dr["OurOpenID"]);
                info.AuthorizationAppID = DataConvert.ToString(dr["AuthorizationAppID"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static OpenidInfo Model(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                return Model(dt.Rows[0], dt.Columns);
            }
            else
            {
                return new OpenidInfo();
            }
        }
        private static List<OpenidInfo> ListModel(DataTable dt)
        {
            List<OpenidInfo> list = new List<OpenidInfo>();
            DataColumnCollection col = dt.Columns;
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Model(row, col));
            }
            return list;
        }
    }
}

